"use strict";
const nodemailer = require("nodemailer");

// async..await is not allowed in global scope, must use a wrapper
// async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
//   let testAccount = await nodemailer.createTestAccount();

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    host: "smtp.qq.com", //发送方使用的邮箱
    port: 465, //端口号  nodemailer/lib/wellknow/services.json
    secure: true, // true for 465, false for other ports
    auth: {

        user: '290204957@qq.com', // 发送方的邮箱地址
        pass: 'gbegfrfeffancbaa', // generated ethereal password  mtp验证码
    },
});

//邮件信息


function send(mail, code) {
    let mailobj = {
        from: '"Fred Foo 👻" <290204957@qq.com>', // sender address
        // to: "290204957@qq.com", // list of receivers
        to: mail,
        subject: "验证信息", // Subject line
        text: `【泽枫】${code}(登陆验证码,请完成验证)，如非本人操作，请忽略本短信`, // plain text body
        // html: "<b>Hello world?</b>", // html body
    }
    return new Promise((reslove, reject) => {
        transporter.sendMail(mailobj, (err, data) => {
            if (err) {
                reject()
            } else {
                reslove()
            }
        })
    })
}

//发送邮件
// transporter.sendMail(mailobj, (err, data) => {

//     })
// send mail with defined transport object
//   let info = await transporter.sendMail({
//     from: '"Fred Foo 👻" <foo@example.com>', // sender address
//     to: "bar@example.com, baz@example.com", // list of receivers
//     subject: "Hello ✔", // Subject line
//     text: "Hello world?", // plain text body
//     html: "<b>Hello world?</b>", // html body
//   });

//   console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

// Preview only available when sending through an Ethereal account
//   console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
// }

// main().catch(console.error);
module.exports = { send }